25 November 2020

About this Document

This is an R Markdown presentation and it is created using Rstudio.

RStudio is an integrated development environment (IDE) for R. RStudio is available in open source and commercial editions and runs on the desktop (Windows, Mac, and Linux).

R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. R is available as Free Software.

Steps

  • Determine the libraries to use
  • Read the file and transform the data
  • Select the more appropriate kind of chart to represent the information
  • Create a dynamic timeline chart

Libraries Used

R packages are a collection of R functions, compiled code and sample data. They are stored under a directory called “library” in the R environment. By default, R installs a set of packages during installation. More packages are added later when they are needed for some specific purpose.

library(readxl) #Read the data from excel sheet
library(DT) #Show a dynamic table
library(highcharter) #Represent the data in timeseries chart

Load the Table and any other Changes in Data

#Load the table, establishing the sheet, range and column types of the data

Turtle <- read_excel("E:/Trabajos Fran/Ostional/Final.xlsx", 
                     sheet = "Chart", range = cell_cols("A1:C43"), 
                     col_types = c("numeric", "numeric", "date")) 

#Change column names

names(Turtle)[1] <- "egglaying"
names(Turtle)[2] <- "nharv"

#Convert Date column to Timeseries column

turtle.date <- ts(data=Turtle, 
                  start = c(2006,06), 
                  end = c(2010,12), 
                  frequency = 12)

Create a visualization of the Table

table <- datatable(turtle.date, extensions = c('Buttons','FixedColumns', 'RowReorder'),
  filter = list(position = 'top', clear = FALSE),
  options = list(dom = 'Bfrtip',
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), dom = 't',
    scrollX = TRUE,
    fixedColumns = TRUE,
    rowReorder = TRUE, order = list(c(0 , 'asc'))))

Consult the Table

Create the TimeSeries Dynamic Chart

#Create the Timeseries chart

Chart <- highchart(type = "stock") %>% 
  hc_title(text = "Total number of Egg Laying Females and Percent of Nests Harvested
           <br>Ostional - Costa Rica, Period 2006-2010</br>") %>% 
  hc_subtitle(text = "Figure Adapted from: Valverde et al. 2012") %>% 
  hc_yAxis_multiples(list(title = list(text = "Egg Laying Females (total number)"), 
                          opposite = FALSE),
                    list(showLastLabel = FALSE, opposite = TRUE, 
                            title = list(text = "Nests Harvested (percent)"))) %>% 
  hc_add_series(turtle.date[,2],  yAxis = 1, type = "column", 
                pointInterval= 30*24*3600*1000, 
                name="Percentage of Nests Harvested", color = "#2F528F")%>%
  hc_add_series(turtle.date[,1],  yAxis = 0, type = "spline", 
                pointInterval= 30*24*3600*1000, 
                name="Egg Laying Females", color = "#ED7D31")%>%
  hc_plotOptions(spline = list(marker = list(enabled = TRUE, radius = 4))) %>% 
  hc_xAxis(title= list(text='Date'), type='datetime')

TimeSeries Dynamic Chart